# coding:utf-8

import requests
from bs4 import BeautifulSoup
import datetime
import json


class ShenTong:

    def __init__(self, start_pro, start_city, start_dis, des_pro, des_city, des_dis):
        self.data = {
            "StartCity": '/'.join([start_pro, start_city, start_dis]),
            "EndCity": '/'.join([des_pro, des_city, des_dis]),
            "StartDatetime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "Weight": 1
        }

        self.headers = {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }

    def get_price(self):
        self.data["Weight"] = 1
        r = requests.post('http://www.sto.cn/Service/GetPriceMessage', data=self.data, headers=self.headers)
        res_first = json.loads(r.text)
        first_price = res_first["Data"]["Price"]
        self.data["Weight"] = 2
        r = requests.post('http://www.sto.cn/Service/GetPriceMessage', data=self.data, headers=self.headers)
        res_second = json.loads(r.text)
        second_price = res_second["Data"]["Price"]
        delta_time = datetime.datetime.strptime(res_second["Data"]["Time"], "%Y-%m-%d %H:%M:%S") - datetime.datetime.now()
        hour = delta_time.days * 24 + delta_time.seconds / 3600.0
        return [first_price, second_price - first_price, hour]
